// pages/mine_message/mine_message.js
const userinfo = wx.getStorageSync('userinfo')
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        username:'',
        userhead:'',
        change:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userhead:wx.getStorageSync('userinfo').avatarUrl,
            username:wx.getStorageSync('userinfo').name
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            userhead:wx.getStorageSync('userinfo').avatarUrl,
            username:wx.getStorageSync('userinfo').name
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    pic:function(){
        wx.chooseImage({
            count: 1,
            sizeType:['compressed','original'],
            success:res=>{
                //上传成功则使用SHOWTOAST
                let path = res.tempFilePaths[0]
                console.log(path)
                wx.showToast({
                  title: '上传中……',
                  mask:true,
                  icon:'loading'
                })
                wx.cloud.uploadFile({
                    cloudPath:`cover/head/${Date.now()}-${Math.floor(Math.random(0, 1) * 10000000)}.png`,
                    filePath:path,
                    success:res=>{
                        console.log(res.fileID)
                        db.collection('user_info').where({
                            _openid:userinfo._openid
                        }).update({
                            data:{
                                avatarUrl:res.fileID
                            }
                        })
                        .then(res=>{
                              db.collection('user_info').where({
                                  _openid:userinfo._openid
                              }).get()
                              .then(res=>{
                                  let data = res.data[0]
                                  wx.setStorageSync('userinfo', data)
                                  console.log(wx.getStorageSync('userinfo'))
                              })
                              .then(res=>{
                                this.setData({
                                    userhead:wx.getStorageSync('userinfo').avatarUrl,
                                    username:wx.getStorageSync('userinfo').name
                                })
                                wx.showToast({
                                    title: '上传成功',
                                    icon:'success',
                                    mask:true
                                  })
                              })
                        })
                    }
                })
            }
        })
    },
    none:function(){

    },
    input:function(){
        if(this.data.username.length != 0){
            this.setData({
                change:true
            })
        }
        else{
            this.setData({
                change:false
            })
        }
    },
    change:function(){
        let that = this
        db.collection('user_info').where({
            openid:wx.getStorageSync('userinfo')._openid
        }).update({
            data:{
                name:that.data.username
            }
        })
        .then(res=>{
            db.collection('user_info').where({
                openid:wx.getStorageSync('userinfo')._openid
            }).get()
            .then(res=>{
                wx.setStorageSync('userinfo', res.data[0])
                wx.showToast({
                    title: '修改成功',
                    mask:true,
                    icon:'success'
                })
                that.setData({
                    username:wx.getStorageSync('userinfo').name
                })
            })
        })
        
    }
})